import java.util.LinkedList;
import java.util.List;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class levelOrder {
    public List<List<Integer>> _levelOrder(TreeNode root) {
        if (root == null)
            return new int[0];
        List<Integer> res = new ArrayList<>();
        LinkedList<TreeNode> link = new LinkedList<>();
        link.add(root);

        while (link.size() != 0) {
            int size = link.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = link.removeFirst();
                res.add(node.val);
                if (node.left != null)
                    link.add(node.left);
                if (node.right != null)
                    link.add(node.right);
            }
        }
        int[] ans = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            ans[i] = res.get(i);
        }
        return ans;
    }
}
